package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.dao.WorklineMapper;
import com.lucher.mes.dao.WorklinesectionMapper;
import com.lucher.mes.dao.WorklinesectionmachineMapper;
import com.lucher.mes.dao.WorkmachineMapper;
import com.lucher.mes.dao.WorksectionMapper;
import com.lucher.mes.po.Operator;
import com.lucher.mes.po.Workline;
import com.lucher.mes.po.Worklinesection;
import com.lucher.mes.po.Worklinesectionmachine;
import com.lucher.mes.po.Workmachine;
import com.lucher.mes.po.Worksection;

@Controller
@RequestMapping("/workline")  
public class WorklineController extends BaseController{
	@Autowired
	private WorklineMapper worklineDao;
	@Autowired
	private WorksectionMapper worksectionDao;
	@Autowired
	private WorkmachineMapper workmachineDao;
	@Autowired
	private WorklinesectionMapper worklinesectionDao;
	@Autowired
	private WorklinesectionmachineMapper worklinesectionmachineDao;
	
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Workline form) {
		return "workline/findWorklineList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Workline form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = worklineDao.findByCount(form);
		List<Workline> list = worklineDao.findByListForPage(form);
		
		for (Workline workline : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", workline.getId());
			objectMap.put("code", workline.getCode());
			objectMap.put("name", workline.getName());
			objectMap.put("info", workline.getInfo());
			objectMap.put("remark", workline.getRemark());
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addInit")
	public String addInit(Workline form,Model model) {
		model.addAttribute("worksectionlist", worksectionDao.findByList(new Worksection()));
		return "workline/addWorkline";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/save")
	public Map<String,Object>  save(Workline form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getCode())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入编号");
			return responseJson;
		} else {
			Workline oldWorkline = worklineDao.findByCode(form.getCode());
			if (oldWorkline != null) {
				responseJson.put("flag", "0");//操作失败
				responseJson.put("msg", "操作失败，此编号已经存在");
				return responseJson;
			}
		}
		
		String sectioncodes = form.getSectioncodes();
		if(StringUtils.isBlank(sectioncodes)){
			responseJson.put("flag", "0");//操作失败
			responseJson.put("msg", "操作失败，请先选择工段信息");
			return responseJson;
		}
		
		//保存工位机信息
		Integer worklineId = worklineDao.insertSelective(form);
		
		String[] sectioncodeArr = sectioncodes.split(",");
		for (String sectioncode : sectioncodeArr) {
			Worklinesection worklinesection = new Worklinesection();
			worklinesection.setLinecode(form.getCode());
			worklinesection.setSectioncode(sectioncode);
			worklinesectionDao.insertSelective(worklinesection);
		}
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}

	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Workline form,Model model) {
		Workline workline = worklineDao.selectByPrimaryKey(form.getId());
		form.setWorkline(workline);
		
		//查询所有的工段信息
		List<Worksection> worksectionlist = worksectionDao.findByList(new Worksection());
		//查询该产线包括的工段信息
		Worklinesection record = new Worklinesection();
		record.setLinecode(workline.getCode());
		List<Worklinesection> worklinesectionList = worklinesectionDao.findByList(record);
		
		for (Worksection worksection : worksectionlist) {
			for (Worklinesection worklinesection : worklinesectionList) {
				if(worksection.getCode().equals(worklinesection.getSectioncode())){
					worksection.setPageSelected(true);
					break;
				}else{
					continue;
				}
			}
		}
		
		model.addAttribute("worksectionlist", worksectionlist);

		return "workline/updateWorkline";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	public Map<String,Object> update(Workline form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getCode())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，编号不能为空");
			return responseJson;
		} else {
			Workline oldWorkline = worklineDao.findByCode(form.getCode());
			if (oldWorkline != null && !oldWorkline.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，编号已经存在");
				return responseJson;
			}
		}

		String sectioncodes = form.getSectioncodes();
		if(StringUtils.isBlank(sectioncodes)){
			responseJson.put("flag", "0");//操作失败
			responseJson.put("msg", "操作失败，请先选择工段信息");
			return responseJson;
		}
		
		// 修改信息
		int flag = worklineDao.updateByPrimaryKeySelective(form);
		
		//新添加关联的工段
		String[] sectioncodeArr = sectioncodes.split(",");
		
		//查询旧工段LIST
		Worklinesection worklinesectionDemo = new Worklinesection();
		worklinesectionDemo.setLinecode(form.getCode());
		List<Worklinesection> worklinesectionList = worklinesectionDao.findByList(worklinesectionDemo);
		for (Worklinesection worklinesection : worklinesectionList) {
			boolean deleteFlag = true; //本次修改是否已经删除该工段,默认已经删除
			for (String sectioncode : sectioncodeArr) {
				if(sectioncode.equals(worklinesection.getSectioncode())){
					deleteFlag = false;//本次修改未删除
					break;
				}
			}
			//表示该工段已经删除掉，需要删除该工段包含的所有工位机
			if(deleteFlag){
				Worklinesectionmachine worklinesectionmachineDemo = new Worklinesectionmachine();
				worklinesectionmachineDemo.setLinecode(worklinesection.getLinecode());
				worklinesectionmachineDemo.setSectioncode(worklinesection.getSectioncode());
				worklinesectionmachineDao.deleteByLinecodeAndSectioncode(worklinesectionmachineDemo);
			}
		}
		
		//删除以前的工段
		Worklinesection record = new Worklinesection();
		record.setLinecode(form.getCode());
		worklinesectionDao.deleteByLinecode(record);
		
		//添加新的工段
		for (String sectioncode : sectioncodeArr) {
			Worklinesection worklinesection = new Worklinesection();
			worklinesection.setLinecode(form.getCode());
			worklinesection.setSectioncode(sectioncode);
			worklinesectionDao.insertSelective(worklinesection);
		}
		
		responseJson.put("flag", "1");//
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	public Map<String,Object> delete(Workline form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		//删除工位机
		worklineDao.deleteByPrimaryKey(form.getId());
		
		//删除关联的工段
		Worklinesection record = new Worklinesection();
		record.setLinecode(form.getCode());
		worklinesectionDao.deleteByLinecode(record);
		
		//删除关联的工位机
		Worklinesectionmachine record1 = new Worklinesectionmachine();
		record1.setLinecode(form.getCode());
		worklinesectionmachineDao.deleteByLinecode(record1);
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Workline> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Workline workline : formlist) {
			//删除工位机
			worklineDao.deleteByPrimaryKey(workline.getId());
			
			//删除关联的工段
			Worklinesection record = new Worklinesection();
			record.setLinecode(workline.getCode());
			worklinesectionDao.deleteByLinecode(record);
			
			//删除关联的工位机
			Worklinesectionmachine record1 = new Worklinesectionmachine();
			record1.setLinecode(workline.getCode());
			worklinesectionmachineDao.deleteByLinecode(record1);
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 查询工段工位信息
	 */
	@RequestMapping(value = "/findWordsectioninfo")
	public String findWordsectioninfo(Workline form) {
		Workline workline = worklineDao.selectByPrimaryKey(form.getId());
		form.setWorkline(workline);

		return "workline/updateWorkline";
	}
	
	
	/**
	 * 查询产线工段信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findWorklinesectionListJson")
	public Map<String, Object> findWorklinesectionListJson(Worklinesection form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = worklinesectionDao.findByCount(form);
		List<Worklinesection> list = worklinesectionDao.findByListForPage(form);
		
		for (Worklinesection worklinesection : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", worklinesection.getId());
			objectMap.put("linecode", worklinesection.getLinecode());
			Workline workline = worklineDao.findByCode(worklinesection.getLinecode());
			objectMap.put("linename", workline.getName());
			objectMap.put("sectioncode", worklinesection.getSectioncode());
			Worksection worksection = worksectionDao.findByCode(worklinesection.getSectioncode());
			objectMap.put("sectionname", worksection.getName());
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}
	
	/**
	 * 查询产线工段工位机信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findWorkmachineListJson")
	public Map<String, Object> findWorkmachineListJson(Worklinesectionmachine form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = worklinesectionmachineDao.findByCount(form);
		List<Worklinesectionmachine> list = worklinesectionmachineDao.findByListForPage(form);
		
		for (Worklinesectionmachine worklinesectionmachine : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", worklinesectionmachine.getId());
			objectMap.put("linecode", worklinesectionmachine.getLinecode());
			Workline workline = worklineDao.findByCode(worklinesectionmachine.getLinecode());
			objectMap.put("linename", workline.getName());
			objectMap.put("sectioncode", worklinesectionmachine.getSectioncode());
			Worksection worksection = worksectionDao.findByCode(worklinesectionmachine.getSectioncode());
			objectMap.put("sectionname", worksection.getName());
			objectMap.put("machinecode", worklinesectionmachine.getMachinecode());
			objectMap.put("serialnumber", worklinesectionmachine.getSerialnumber());
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}
	
	
	/**
	 * 更新产线关联工位机初始化
	 */
	@RequestMapping(value = "/updatemachineInit")
	public String updatemachineInit(Worklinesection form,Model model) {
		form = worklinesectionDao.selectByPrimaryKey(form.getId());
		model.addAttribute("worklinesection", form);
		model.addAttribute("workline", worklineDao.findByCode(form.getLinecode()));
		model.addAttribute("worksection", worksectionDao.findByCode(form.getSectioncode()));
		
		//查询所有已经被选用的工位机
		List<Worklinesectionmachine> worklinesectionmachineList = worklinesectionmachineDao.findByList(new Worklinesectionmachine());
		
		//查询所有的工位机信息
		List<Workmachine> workmachinelist = workmachineDao.findByList(new Workmachine());
		
		//循环所有的工位机信息
		for (Workmachine workmachine : workmachinelist) {
			for (Worklinesectionmachine worklinesectionmachine : worklinesectionmachineList) {
				if(workmachine.getCode().equals(worklinesectionmachine.getMachinecode())){
					if(worklinesectionmachine.getLinecode().equals(form.getLinecode()) && worklinesectionmachine.getSectioncode().equals(form.getSectioncode())){
						workmachine.setPageSelected(true);
						break;
					}else{
						workmachine.setPageUsed(true);
						break;
					}
				}else{
					continue;
				}
			}
		}
		
		model.addAttribute("workmachinelist", workmachinelist);

		return "workline/updateWorkmachine";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/updatemachine")
	public Map<String,Object> updatemachine(Workline form,Worklinesection record) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");

		String machinecodes = form.getMachinecodes();
		if(StringUtils.isBlank(machinecodes)){
			responseJson.put("flag", "0");//操作失败
			responseJson.put("msg", "操作失败，请先选择工位机信息");
			return responseJson;
		}
		
		record = worklinesectionDao.selectByPrimaryKey(record.getId());
		
		//删除以前的工位机
		Worklinesectionmachine worklinesectionmachineDemo = new Worklinesectionmachine();
		worklinesectionmachineDemo.setLinecode(record.getLinecode());
		worklinesectionmachineDemo.setSectioncode(record.getSectioncode());
		worklinesectionmachineDao.deleteByLinecodeAndSectioncode(worklinesectionmachineDemo);
		
		//新添加关联的工位机
		String[] machinecodeArr = machinecodes.split(",");
		for (String machinecode : machinecodeArr) {
			Worklinesectionmachine worklinesectionmachine = new Worklinesectionmachine();
			worklinesectionmachine.setLinecode(record.getLinecode());
			worklinesectionmachine.setSectioncode(record.getSectioncode());
			worklinesectionmachine.setMachinecode(machinecode);
			worklinesectionmachineDao.insertSelective(worklinesectionmachine);
		}
		
		responseJson.put("flag", "1");//
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
	
	
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/updateOrdering")
	public Map<String,Object> updateOrdering(Worklinesectionmachine record) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		//修改关联的工位机
		worklinesectionmachineDao.updateByPrimaryKeySelective(record);
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
}
